#include <basic/errcode.h>
#include <basic/list.h>

void listp_add(struct listp_link *new_link,
        struct listp_link *prev_link)
{/*{{{*/
    struct listp_link *next_link=prev_link->next;

    next_link->prev=new_link;
    new_link->next=next_link;
    new_link->prev=prev_link;
    prev_link->next=new_link;
}/*}}}*/

void listp_add_tail(struct listp_link *new_link,
        struct listp_link *head_link)
{/*{{{*/
    listp_add(new_link, head_link->prev);
}/*}}}*/

void listp_del(struct listp_link *delete_link)
{/*{{{*/
    struct listp_link *prev_link;
    struct listp_link *next_link;

    prev_link=delete_link->prev;
    next_link=delete_link->next;
    next_link->prev=prev_link;
    prev_link->next=next_link;
}/*}}}*/

void listp_takeover(struct listp_link *releaser_head,
    struct listp_link *receiver_head)
{/*{{{*/
    if(listp_is_empty(releaser_head))
    {
        listp_init(receiver_head);
        return;
    }

    receiver_head->next=releaser_head->next;
    receiver_head->prev=releaser_head->prev;
    receiver_head->next->prev=receiver_head;
    receiver_head->prev->next=receiver_head;
	listp_init(releaser_head);
}/*}}}*/

void listp_append(struct listp_link *append_head,
    struct listp_link *dest_head)
{/*{{{*/
    if(listp_is_empty(append_head))  return;

    dest_head->prev->next=append_head->next;
    append_head->next->prev=dest_head->prev;
    append_head->prev->next=dest_head;
    dest_head->prev=append_head->prev;
    listp_init(append_head);
}/*}}}*/

int listp_count(struct listp_link *list_head)
{/*{{{*/
    struct listp_link *list=list_head;
    int count=0;

    while((list=list->next) != list_head)
        count++; 
    return(count);
}/*}}}*/
